package com.shijie.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.shijie.component.ErpUserService;
import com.shijie.core.AbstractService;
import com.shijie.core.ServiceException;
import com.shijie.domain.entity.Groupons;
import com.shijie.domain.entity.UserGroupons;
import com.shijie.domain.enums.EnumInterface;
import com.shijie.mapper.GrouponsMapper;
import com.shijie.mapper.ProductsMapper;
import com.shijie.mapper.UserGrouponsMapper;

/*
 *  @author: smart boy
 *  @date: 2018-11-21
 */
@Service
public class GrouponsService extends AbstractService<Groupons> {

	@Resource
	private GrouponsMapper grouponsMapper;

	@Resource
	private ErpUserService erpUserService;
	
	@Resource
	private UserGrouponsMapper userGrouponsMapper;

	@Resource
	private ProductsMapper productsMapper;

	public void deleteById(Integer id){
		UserGroupons ug = new UserGroupons();
		ug.setGrouponId(id);
		List<UserGroupons> list= userGrouponsMapper.select(ug);
		if(list.size() > 0 ){
			throw new ServiceException("该团购已被用户使用，不能删除！");
		}
		grouponsMapper.deleteByPrimaryKey(id);
	}

	public void save(Groupons groupons){
		groupons.setCreateBy(erpUserService.getUserId());
		groupons.setCreateDate(DateTime.now().toDate());
		groupons.setCreateName(erpUserService.getUserName());
		grouponsMapper.insertSelective(groupons);
	}

	public List<Groupons> listGroupons(){

		Condition condition = new Condition(Groupons.class);
		Example.Criteria criteria = condition.createCriteria();
		criteria.andLessThanOrEqualTo("startDate", new Date());
		criteria.andGreaterThanOrEqualTo("endDate", new Date());
		condition.orderBy("id").desc();
		List<Groupons> list = grouponsMapper.selectByCondition(condition);
		for (Groupons groupons : list) {
			groupons.setProducts(productsMapper.selectByPrimaryKey(groupons.getProductId()));
			UserGroupons record = new UserGroupons();
			record.setPayStatus(EnumInterface.PayType.WAIT_DELIVERY.ordinal());
			record.setStatus(EnumInterface.Status.VALID.ordinal());
			groupons.setSellNum(userGrouponsMapper.selectCount(record));
		}

		return list;
	}

	public List<UserGroupons> listGrouponsForUser(Integer userId) {

		Condition condition = new Condition(UserGroupons.class);
		Example.Criteria criteria = condition.createCriteria();
		criteria.andEqualTo("userId", userId);
		criteria.andEqualTo("status", EnumInterface.Status.VALID.ordinal());
		condition.orderBy("id").desc();
		List<UserGroupons> list = userGrouponsMapper.selectByCondition(condition);
		for (UserGroupons userGroupons : list) {
			Groupons groupon = grouponsMapper.selectByPrimaryKey(userGroupons.getGrouponId());
			userGroupons.setGroupons(groupon);
			userGroupons.setProducts(productsMapper.selectByPrimaryKey(groupon.getProductId()));
		}
		return list;
	}

	public String insertUserGroupons(UserGroupons userGroupon) {
		if (userGroupon.getGrouponId() == null) {
			return "请输入参加的团购ID!";
		}
		Groupons groupons = grouponsMapper.selectByPrimaryKey(userGroupon.getGrouponId());
		if (groupons != null && groupons.getStartDate().compareTo(new Date()) < 1 && new Date().compareTo(groupons.getEndDate()) < 1) {

			//TODO 需要修正支付的逻辑，还要涉及微信退款
			

		} else {
			return "请核实是否存在该团购，如果存在请核实该团购尚未开始或已经结束！";
		}
		return null;
	}
}
